package com.jintoufs.service.trade.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jintoufs.dao.baseinfo.SiteCoinDAO;
import com.jintoufs.dao.baseinfo.TradeDAO;
import com.jintoufs.dao.trade.WithdrawDAO;
import com.jintoufs.domain.trade.Withdraw;
import com.jintoufs.domain.baseinfo.SiteCoin;
import com.jintoufs.domain.baseinfo.Trade;
import com.jintoufs.domain.constants.Constants;
import com.jintoufs.service.trade.CoinwTradeService;
import com.jintoufs.util.http.CoinwHttpsUtil;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiaoemo
 */
@Repository
public class CoinwTradeServiceImpl implements CoinwTradeService {
    @Resource
    private TradeDAO tradeDAO;
    @Resource
    private WithdrawDAO withdrawDAO;
    @Resource
    private SiteCoinDAO siteCoinDAO;

    @Override
    public AjaxMsg coinwTrade(Trade trade) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        ajaxMsg.setDatas(AjaxMsg.FAILURE);
        Integer coinId = trade.getCoinId();
        Integer siteId = trade.getSiteId();
        Map map = new HashMap();
        map.put("siteId", siteId);
        map.put("coinId", coinId);
        List<Map<String, Object>> list = siteCoinDAO.queryByParams(map);
        if (list != null && list.size() > 0) {
            String SYMBOL = list.get(0).get("price_api").toString().split("symbol=")[1];
            String api = "https://www.coinw.me/appApi.html?action=trade&symbol=SYMBOL&type=TYPE&amount=AMOUNT&price=PRICE";
            api.replace("SYMBOL", SYMBOL);
            api.replace("TYPE", trade.getType());
            api.replace("AMOUNT", String.valueOf(trade.getAmount()));
            api.replace("PRICE", String.valueOf(trade.getPrice()));
            String json = CoinwHttpsUtil.getStringByHttpsPost(api);
            JSONObject object = JSON.parseObject(json);
            if (object.get("code").toString().equals("200")) {
                String id = object.get("data").toString();
                trade.setTid(id);
                trade.setStatus(Constants.TRADE_STATUS_NEW);
                tradeDAO.updateByPrimaryKey(trade);
                ajaxMsg.setDatas(AjaxMsg.SUCCESS);
            }else {
                ajaxMsg.setDatas(AjaxMsg.FAILURE);
            }
        }
        return ajaxMsg;
    }

    @Override
    public AjaxMsg coinwTradeCancel(Trade trade) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        ajaxMsg.setDatas(AjaxMsg.FAILURE);

        String api = "https://www.coinw.me/appApi.html?action=cancel_entrust&id=ID";
        api.replace("ID", trade.getTid());
        String json = CoinwHttpsUtil.getStringByHttpsPost(api);
        JSONObject object = JSON.parseObject(json);
        if (object.get("code").toString().equals("200")) {
            ajaxMsg.setDatas(AjaxMsg.SUCCESS);
            trade.setStatus(Constants.TRADE_STATUS_CANCEL);
            tradeDAO.updateByPrimaryKey(trade);
        }
        return ajaxMsg;
    }

    @Override
    public AjaxMsg coinwTradeStatus(Trade trade) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        ajaxMsg.setDatas(AjaxMsg.FAILURE);

        String api = "https://www.coinw.me/appApi.html?action=order&id=ID";
        api.replace("ID", trade.getTid());
        String json = CoinwHttpsUtil.getStringByHttpsPost(api);
        JSONObject object = JSON.parseObject(json);
        if (object.get("code").toString().equals("200")) {
            String data = object.get("data").toString();
            JSONObject dataObj = JSON.parseObject(data);
            if(dataObj.get("status").toString().equals("OK")){
                ajaxMsg.setDatas(AjaxMsg.SUCCESS);
                trade.setStatus(Constants.TRADE_STATUS_OK);
                tradeDAO.updateByPrimaryKey(trade);
            }
        }
        return ajaxMsg;
    }
    @Override
    public AjaxMsg coinwWithdraw(Withdraw withdraw) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        ajaxMsg.setDatas(AjaxMsg.FAILURE);
        Map map = new HashMap();
        SiteCoin siteCoin = siteCoinDAO.selectByPrimaryKey(withdraw.getSiteCoinId());
        if (siteCoin!=null) {
            String api = "https://www.coinw.me/appApi.html?action=withdraw&symbol=SYMBOL&amount=AMOUNT&withdrawaddress=WITHDRAWADDRESS";
            String SYMBOL = siteCoin.getPriceApi().split("symbol=")[1];
            api.replace("SYMBOL", SYMBOL);
            api.replace("AMOUNT", String.valueOf(withdraw.getAmount()));
            api.replace("WITHDRAWADDRESS", String.valueOf(withdraw.getWithdrawAddress()));
            String json = CoinwHttpsUtil.getStringByHttpsPost(api);
            JSONObject object = JSON.parseObject(json);
            if (object.get("code").toString().equals("200")) {
                String id = object.get("data").toString();
                withdraw.settId(id);
                withdraw.setStatus(Constants.TRADE_STATUS_NEW);
                withdrawDAO.updateByPrimaryKey(withdraw);
                ajaxMsg.setDatas(AjaxMsg.SUCCESS);
            }
        }
        return ajaxMsg;
    }

    @Override
    public AjaxMsg coinwWithdrawCancel(Withdraw withdraw) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        ajaxMsg.setDatas(AjaxMsg.FAILURE);
        Map map = new HashMap();
        SiteCoin siteCoin = siteCoinDAO.selectByPrimaryKey(withdraw.getSiteCoinId());
        if (siteCoin!=null) {
            String api = "https://www.coinw.me/appApi.html?action=cancel_withdraw&id=ID";
            api.replace("ID", withdraw.gettId());
            String json = CoinwHttpsUtil.getStringByHttpsPost(api);
            JSONObject object = JSON.parseObject(json);
            if (object.get("code").toString().equals("200")) {
                String id = object.get("data").toString();
                withdraw.settId(id);
                withdraw.setStatus(Constants.TRADE_STATUS_CANCEL);
                withdrawDAO.updateByPrimaryKey(withdraw);
                ajaxMsg.setDatas(AjaxMsg.SUCCESS);
            }
        }
        return ajaxMsg;
    }
}
